﻿using FlyingEye.Common;
using FlyingEye.Consts;
using FlyingEye.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using Volo.Abp.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class RabbitMQClientController : AbpController
    {
        private readonly RabbitMQProxy _proxy;

        private readonly ILogger<RabbitMQClientController> _logger;

        public RabbitMQClientController(RabbitMQProxy proxy, ILogger<RabbitMQClientController> logger)
        {
            _proxy = proxy;
            _logger = logger;
        }

        /// <summary>
        /// 发布消息到 web 页面
        /// </summary>
        /// <param name="processName">工序名称</param>
        /// <param name="jsonMessage">json 消息</param>
        [HttpPost]
        [Authorize(Roles = $"{UserRoleConsts.ME},{UserRoleConsts.Admin}")]
        public DefaultActionResult PostMessageToWebclient(string processName, string jsonMessage)
        {
            try
            {
                JObject.Parse(jsonMessage);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Json 消息格式错误。");
                return DefaultActionResult.GetFailureResult("Json 消息格式错误。");
            }

            if (Enum.TryParse<ProcessNames>(processName, out var name))
            {
                try
                {
                    _proxy.PostMessageToWebclient(name, jsonMessage);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "RabbitMQ 消息发布异常。");
                    return DefaultActionResult.GetFailureResult("RabbitMQ 消息发布异常。");
                }

                return DefaultActionResult.GetSuccessResult();
            }
            else
            {
                return DefaultActionResult.GetFailureResult("RabbitMQ 主机名字不存在。");
            }
        }

        /// <summary>
        /// 发布消息到极片分类
        /// </summary>
        /// <param name="jsonMessage">json格式消息</param>
        [HttpPost]
        [Authorize(Roles = $"{UserRoleConsts.ME},{UserRoleConsts.Admin}")]
        public DefaultActionResult PostMessageToMaterialClassification(string jsonMessage)
        {
            try
            {
                JObject.Parse(jsonMessage);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Json 消息格式错误。");
                return DefaultActionResult.GetFailureResult("Json 消息格式错误。");
            }

            try
            {
                _proxy.PostMessageToMaterialClassification(jsonMessage);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "RabbitMQ 消息发布异常。");
                return DefaultActionResult.GetFailureResult("RabbitMQ 消息发布异常。");
            }

            return DefaultActionResult.GetSuccessResult();
        }
    }
}
